package com.canlihao.ui.view.activity;

import android.os.Bundle;
import android.view.View;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.canlihao.ui.R;
import com.canlihao.ui.base.BaseWhiteTopbarActivity;
import com.canlihao.ui.utils.ToastUtil;
import com.canlihao.ui.utils.bannerUtil.LocalImageHolderView;
import com.canlihao.ui.utils.bannerUtil.NetGlidHolderView;
import com.canlihao.ui.utils.bannerUtil.NetWorkImageHolderView;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import me.relex.photodraweeview.OnViewTapListener;

/*******
 *
 *Created by panda李
 *
 *创建时间 2020/2/7
 *
 *类描述：
 *
 ********/
public class BannerActivity extends BaseWhiteTopbarActivity implements OnItemClickListener, OnViewTapListener {
    @BindView(R.id.convenientBannerOne)
    ConvenientBanner convenientBannerOne;
    @BindView(R.id.convenientBannerTwo)
    ConvenientBanner convenientBannerTwo;
    @BindView(R.id.convenientBannerThree)
    ConvenientBanner convenientBannerThree;
    private ArrayList<Integer> localImages = new ArrayList<Integer>();
    private ArrayList<String> imagess = new ArrayList<>();
    private void loadTestDatass() {
        //图片可能过期哦，自己换来测试吧
        imagess.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1525319864&di=87f476652c96678547ccabbf112076be&imgtype=jpg&er=1&src=http%3A%2F%2Fimg.pconline.com.cn%2Fimages%2Fupload%2Fupc%2Ftx%2Fgamephotolib%2F1410%2F27%2Fc0%2F40170771_1414341013392.jpg");
        imagess.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1524714908870&di=9d43d35cefbabacdc879733aa7ddc82b&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimage%2Fc0%253Dshijue1%252C0%252C0%252C294%252C40%2Fsign%3D46de93bfc711728b24208461a095a9bb%2F4610b912c8fcc3ce5423d51d9845d688d43f2038.jpg");
        imagess.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1524714935901&di=052557513540f3d740eeeb2439c585bb&imgtype=0&src=http%3A%2F%2Fwww.gzlco.com%2Fimggzl%2F214%2F1b6e6520ca474fe4bd3ff728817950717651.jpeg");
    }

    @Override
    protected void initView() {
        super.initView();
        mTopBar.setTitle("banner");
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_banner;
    }

    @Override
    public void initData() {
        super.initData();
        locaImg();
        netImg();

    }
    private void netImg() {
        //网络加载例子
        loadTestDatass();
        convenientBannerTwo.setPages(
                new CBViewHolderCreator() {
                    @Override
                    public NetWorkImageHolderView createHolder(View itemView) {
                        return new NetWorkImageHolderView(itemView, BannerActivity.this);
                    }

                    @Override
                    public int getLayoutId() {
                        return R.layout.item_net;
                    }
                }, imagess)
                //设置两个点图片作为翻页指示器，不设置则没有指示器，可以根据自己需求自行配合自己的指示器,不需要圆点指示器可用不设
                .setPageIndicator(new int[]{R.drawable.ic_page_indicator, R.drawable.ic_page_indicator_focused});
        convenientBannerThree.setPages(
                new CBViewHolderCreator() {
                    @Override
                    public NetGlidHolderView createHolder(View itemView) {
                        return new NetGlidHolderView(itemView,BannerActivity.this);
                    }

                    @Override
                    public int getLayoutId() {
                        return R.layout.item_localimage;
                    }
                }, imagess)
                //设置两个点图片作为翻页指示器，不设置则没有指示器，可以根据自己需求自行配合自己的指示器,不需要圆点指示器可用不设
                .setPageIndicator(new int[]{R.drawable.ic_page_indicator, R.drawable.ic_page_indicator_focused})
                .setOnItemClickListener(this);
    }

    private void locaImg() {
        loadTestDatas();
        convenientBannerOne.setPages(
                new CBViewHolderCreator() {
                    @Override
                    public LocalImageHolderView createHolder(View itemView) {
                        return new LocalImageHolderView(itemView);
                    }

                    @Override
                    public int getLayoutId() {
                        return R.layout.item_localimage;
                    }
                }, localImages)
                //设置两个点图片作为翻页指示器，不设置则没有指示器，可以根据自己需求自行配合自己的指示器,不需要圆点指示器可用不设
                .setPageIndicator(new int[]{R.drawable.ic_page_indicator, R.drawable.ic_page_indicator_focused})
                .setOnItemClickListener(this);
    }

    private void loadTestDatas() {
        //本地图片集合
        for (int position = 0; position < 7; position++)
            localImages.add(getResId("ic_test_" + position, R.drawable.class));
    }

    public static int getResId(String variableName, Class<?> c) {
        try {
            Field idField = c.getDeclaredField(variableName);
            return idField.getInt(idField);
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    // 开始自动翻页
    @Override
    protected void onResume() {
        super.onResume();
//        //开始自动翻页
        convenientBannerOne.startTurning();
        convenientBannerThree.startTurning();
    }

    // 停止自动翻页
    @Override
    protected void onPause() {
        super.onPause();
//        //停止翻页
        convenientBannerOne.stopTurning();
        convenientBannerThree.stopTurning();
    }

    @Override
    public void onItemClick(int position) {
        ToastUtil.showToast(this, "点击了第" + position + "个");
    }

    @Override
    public void onViewTap(View view, float x, float y) {
        finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
